## Loading packages
install.packages(c("tidyverse", "texreg"))
library(tidyverse)
library(texreg)

## Loading datasets
load("leg_x_xi.RData")
load("leg_xv_xvi.RData")

## Table 1
leg_one = glm(renominated ~ la_stampa_corrupt_log*leg_xi, data = leg_x_xi, family = "binomial")

leg_one_controls = glm(renominated ~ la_stampa_corrupt_log*leg_xi + 
                         la_stampa_base_log + parl_exp_nterms + elite + age + job +
                         college + female + governing + south, data = leg_x_xi, family = "binomial")

leg_two = glm(renominated ~ la_stampa_corrupt_log*leg_xvi, data = leg_xv_xvi, family = "binomial")

leg_two_controls = glm(renominated ~ la_stampa_corrupt_log*leg_xvi + 
                         la_stampa_base_log + parl_exp_nterms + elite + age + job +
                         college + female + governing + south, data = leg_xv_xvi,
                       family = "binomial")

texreg(list(leg_one, leg_one_controls,
            leg_two, leg_two_controls),
       label = "main",
       caption = "Corruption Mentions and Renomination",
       custom.model.names = c("(1)", "(2)", "(3)", "(4)"),
       custom.coef.names = c("Intercept", "log(Press Mentions)",
                             "Legislature XI", "log(Press Mentions) x Legislature XI",
                             "log(Base Mentions)", "Seniority", "Elite",
                             "Age", "Job", "College", "Female", "Governing Party",
                             "South", "Legislature XVI",
                             "log(Press Mentions) x Legislature XVI"),
       reorder.coef = c(2, 3, 14, 5, 6, 7, 8, 9, 10, 11, 12, 13, 4, 15, 1),
       include.aic = FALSE,
       include.bic = FALSE,
       include.loglik = FALSE,
       include.deviance = FALSE,
       custom.gof.names = c("Observations"),
       stars = c(0.10, 0.05, 0.01, 0.001),
       digits = 3,
       center = TRUE,
       include.ci = FALSE,
       fontsize = "footnotesize",
       caption.above = TRUE)

## Figure 4
leg_one_controls_shell = leg_x_xi %$%
  expand.grid(la_stampa_corrupt_log = c(min(la_stampa_corrupt_log, na.rm = TRUE),
                                        max(la_stampa_corrupt_log, na.rm = TRUE)),
              leg_xi = c(0, 1),
              la_stampa_base_log = mean(la_stampa_base_log, na.rm = TRUE),
              governing = mean(governing, na.rm = TRUE),
              parl_exp_nterms = mean(parl_exp_nterms, na.rm = TRUE),
              elite = mean(elite, na.rm = TRUE),
              south = mean(south, na.rm = TRUE),
              age = mean(age, na.rm = TRUE),
              job = mean(job, na.rm = TRUE),
              college = mean(college, na.rm = TRUE),
              female = mean(female, na.rm = TRUE)) %>% 
  na.omit()

pred_leg_one_controls = cbind(predict(leg_one_controls, leg_one_controls_shell, 
                                      se.fit = TRUE, type = "response"), 
                              leg_one_controls_shell)

plot_leg_one_controls = ggplot(pred_leg_one_controls, aes(x = factor(leg_xi), 
                         y = fit,
                         ymin = fit - (se.fit * 1.96),
                         ymax = fit + (se.fit * 1.96),
                         color = factor(la_stampa_corrupt_log))) +  
  geom_pointrange(size = .7, position = position_dodge(width = .5)) + 
  scale_color_manual(values = c("grey", "black"), labels = c("0.00 (min.)", "5.38 (max.)")) +
  scale_x_discrete(labels = c("X", "XI")) +
  scale_y_continuous(labels = scales::number_format(accuracy = 0.01)) +
  labs(y = "Pr(Renomination)", x = "Legislature", 
       color = "log(Press Mentions)") +
  theme_classic() +
  theme(legend.position = "bottom")

leg_two_controls_shell = leg_xv_xvi %$%
  expand.grid(la_stampa_corrupt_log = c(min(la_stampa_corrupt_log, na.rm = TRUE),
                                        max(la_stampa_corrupt_log, na.rm = TRUE)),
              leg_xvi = c(0, 1),
              la_stampa_base_log = mean(la_stampa_base_log, na.rm = TRUE),
              governing = mean(governing, na.rm = TRUE),
              parl_exp_nterms = mean(parl_exp_nterms, na.rm = TRUE),
              elite = mean(elite, na.rm = TRUE),
              south = mean(south, na.rm = TRUE),
              age = mean(age, na.rm = TRUE),
              job = mean(job, na.rm = TRUE),
              college = mean(college, na.rm = TRUE),
              female = mean(female, na.rm = TRUE)) %>% 
  na.omit()

pred_leg_two_controls = cbind(predict(leg_two_controls, leg_two_controls_shell, 
                                      se.fit = TRUE, type = "response"), 
                              leg_two_controls_shell)

plot_leg_two_controls = ggplot(pred_leg_two_controls, aes(x = factor(leg_xvi), 
                                  y = fit,
                                  ymin = fit - (se.fit * 1.96),
                                  ymax = fit + (se.fit * 1.96),
                                  color = factor(la_stampa_corrupt_log))) +  
  geom_pointrange(size = .7, position = position_dodge(width = .5)) + 
  scale_color_manual(values = c("grey", "black"), labels = c("0.00 (min.)", "4.26 (max.)")) +
  scale_x_discrete(labels = c("XV", "XVI")) +
  scale_y_continuous(labels = scales::number_format(accuracy = 0.01)) +
  labs(y = "Pr(Renomination)", x = "Legislature", 
       color = "log(Press Mentions)") +
  theme_classic() +
  theme(legend.position = "bottom")

## Table A2
leg_one_maj = glm(renominated ~ la_stampa_corrupt_log*leg_xi*maj_party, data = leg_x_xi, family = "binomial")

leg_one_controls_maj = glm(renominated ~ la_stampa_corrupt_log*leg_xi*maj_party +
                             la_stampa_base_log + parl_exp_nterms + elite + age + job +
                             college + female + governing + south, data = leg_x_xi, family = "binomial")

leg_two_maj = glm(renominated ~ la_stampa_corrupt_log*leg_xvi*maj_party, data = leg_xv_xvi, family = "binomial")

leg_two_controls_maj = glm(renominated ~ la_stampa_corrupt_log*leg_xvi*maj_party + la_stampa_base_log + 
                             parl_exp_nterms + elite + age + job + college + 
                             female + governing + south, data = leg_xv_xvi, family = "binomial")

texreg(list(leg_one_maj, leg_one_controls_maj,
            leg_two_maj, leg_two_controls_maj),
       label = "maj_party",
       caption = "Corruption Mentions and Renomination -- Major Parties vs. Minor Parties",
       custom.model.names = c("(1)", "(2)", "(3)", "(4)"),
       custom.coef.names = c("Intercept", "log(Press Mentions)",
                             "Legislature XI", "Major Party", 
                             "log(Press Mentions) x Legislature XI",
                             "log(Press Mentions) x Major Party",
                             "Legislature XI x Major Party",
                             "log(Press Mentions) x Legislature XI x Major Party",
                             "Legislature XVI",
                             "log(Press Mentions) x Legislature XVI",
                             "Legislature XVI x Major Party",
                             "log(Press Mentions) x Legislature XVI x Major Party"),
       omit.coef = "(la_stampa_base_log)|(parl_exp_nterms)|(elite)|(age)|(job)|(college)|(female)|(governing)|(south)",
       reorder.coef = c(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1),
       include.aic = FALSE,
       include.bic = FALSE,
       include.loglik = FALSE,
       include.deviance = FALSE,
       custom.gof.names = c("Observations"),
       stars = c(0.10, 0.05, 0.01, 0.001),
       digits = 3,
       center = TRUE,
       include.ci = FALSE,
       fontsize = "small",
       caption.above = TRUE)
